package com.itsu.mysql.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itsu.interfaces.entity.PublishState;
import com.itsu.interfaces.service.PublishService;
import com.itsu.interfaces.service.PublishStateService;
import com.itsu.mysql.mappers.PublishStateMapper;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class PublishStateServiceImpl implements PublishStateService {

    @Autowired
    PublishStateMapper publishStateMapper;

    @Override
    public void save(PublishState publishState) {
        publishStateMapper.insert(publishState);
    }

    @Override
    public PublishState findByPublishId(String publishId) {
        return publishStateMapper.selectOne(Wrappers.lambdaQuery(PublishState.class).eq(PublishState::getPublishId, publishId));
    }

    @Override
    public List<PublishState> findByUseId(Long userId) {
        return publishStateMapper.selectList(Wrappers.lambdaQuery(PublishState.class).eq(PublishState::getUserId, userId));
    }

    @Override
    public List<PublishState> findByPublishIds(List<String> publishIds) {
        return publishStateMapper.selectList(Wrappers.lambdaQuery(PublishState.class).in(PublishState::getPublishId, publishIds));
    }

    @Override
    public int updatePublishStateByPublishId(PublishState publishState) {
        return publishStateMapper.update(publishState, Wrappers.lambdaUpdate(PublishState.class)
                .eq(PublishState::getPublishId, publishState.getPublishId()));
    }

    @Override
    public int updatePublishStateByPublishIds(List<String> publishIds, String state) {
        UpdateWrapper<PublishState> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("publish_id", publishIds);
        updateWrapper.set("state", state);
        return publishStateMapper.update(null, updateWrapper);
    }
}
